package com.nf.health.app.inquiry.impl.viewholder;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.customview.CircleImageView;
import com.nf.health.app.customview.MyFlowLayout;
import com.nf.health.app.glide.GlideUtil;
import com.nf.health.app.inquiry.bean.DoctorDetailBean;
import com.nf.health.app.utils.NetUtil;
import com.nf.health.app.utils.ViewUtil;
import com.nf.health.app.widget.OnMyOnClistener2;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 主界面界面 医生详情
 * 头像，简介
 *
 * @author yyh
 *         Created by Jason on 2016/9/21.
 */
public class DoctorHeaderViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    @Bind(R.id.iv_head_icon_doc)
    public CircleImageView iv_head_icon_doc;
    @Bind(R.id.doctor_name_doc)//医生姓名
    public TextView doctor_name_doc;
    @Bind(R.id.doctor_hotipal_address)
    TextView doctor_hotipal_address;//医院名称
    @Bind(R.id.doctor_job_doc)
    TextView doctor_job_doc;//专业
    @Bind(R.id.doctor_hospital_doc)//医院名称
            MyFlowLayout doctor_hospital_doc;
    @Bind(R.id.btn_foucus_doc)//关注或是取消关注
            TextView btn_foucus_doc;
    @Bind(R.id.tv_evaluation_rate)//好平lu
            TextView tv_evaluation_rate;
    @Bind(R.id.tv_sign_contract)//签约家庭
            TextView tv_sign_contract;
    @Bind(R.id.tv_healthy_manager)//健康管理
            TextView tv_healthy_manager;
    @Bind(R.id.tv_pay_money_ask)//付费咨询
            TextView tv_pay_money_ask;
    @Bind(R.id.doctor_good_doc)//医生类别（住院医生）
            TextView doctor_good_doc;

    @Bind(R.id.tv_doctor_goodness_doc)//擅长
            TextView tv_doctor_goodness_doc;

    @Bind(R.id.tv_doctor_introduction_doc)//简介
            TextView tv_doctor_introduction_doc;
    @Bind(R.id.tv_query_doc_detail)//查看详情
            TextView tv_query_doc_detail;
    @Bind(R.id.ll_shouqi_icon_doc)//查看详情ll
            LinearLayout ll_shouqi_icon_doc;
    private String attention;

    private Activity cnt;

    public DoctorHeaderViewHolder(View itemView, Activity cnt, OnMyOnClistener2 onMyOnClistener) {
        super(itemView);
        ButterKnife.bind(this, itemView);
        this.cnt = cnt;
        this.onMyOnClistener = onMyOnClistener;
        btn_foucus_doc.setOnClickListener(this);
        ll_shouqi_icon_doc.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_shouqi_icon_doc:
            case R.id.tv_query_doc_detail:
                Integer tag = (Integer) tv_query_doc_detail.getTag();
                if (tag == null) {
                    tv_query_doc_detail.setText("查看详情");//查看详情
                    tv_doctor_introduction_doc.setMaxLines(1);
                    tv_doctor_goodness_doc.setMaxLines(1);
                    tv_doctor_introduction_doc.setEllipsize(TextUtils.TruncateAt.END);
                    tv_doctor_goodness_doc.setEllipsize(TextUtils.TruncateAt.END);
                    tv_query_doc_detail.setTag(R.drawable.arrow_down);
                    ViewUtil.setTextImgBackground(tv_query_doc_detail, cnt, ViewUtil.TextMode.RIGHT, 0, R.drawable.arrow_down, 20);
                } else if (tag == R.drawable.arrow_up) {
                    tv_query_doc_detail.setText("查看详情");//查看详情
                    tv_query_doc_detail.setTag(R.drawable.arrow_down);
                    tv_doctor_introduction_doc.setMaxLines(1);
                    tv_doctor_goodness_doc.setMaxLines(1);
                    ViewUtil.setTextImgBackground(tv_query_doc_detail, cnt, ViewUtil.TextMode.RIGHT, 0, R.drawable.arrow_down, 20);
                } else if (tag == R.drawable.arrow_down) {
                    tv_query_doc_detail.setText("向上收起");//查看详情
                    tv_query_doc_detail.setTag(R.drawable.arrow_up);
                    tv_doctor_introduction_doc.setMaxLines(20);
                    tv_doctor_goodness_doc.setMaxLines(20);
                    ViewUtil.setTextImgBackground(tv_query_doc_detail, cnt, ViewUtil.TextMode.RIGHT, 0, R.drawable.arrow_up, 20);
                }
                break;
            case R.id.btn_foucus_doc:
                if (onMyOnClistener != null) {
                    String attention = getAttention();
                    if (!TextUtils.isEmpty(attention)) {
                        onMyOnClistener.onMyClickCallBack(attention);
                    }
                }
                break;
        }
    }

    private OnMyOnClistener2 onMyOnClistener;//注册点击事件

    public void loadData(DoctorDetailBean doctorDetail) {
        if(!NetUtil.isNetworkAvailable(cnt)){
            doctor_name_doc.setText("");
            doctor_hotipal_address.setText("");
            doctor_job_doc.setText("");
        }
        if (doctorDetail == null) {
            return;
        }
        GlideUtil.loadNetImg2(cnt, iv_head_icon_doc, doctorDetail.getDoctorhimg(), R.drawable.default_head);
        doctor_name_doc.setText(TextUtils.isEmpty(doctorDetail.getDoctorname()) ? "未填写" : doctorDetail.getDoctorname());
        doctor_hotipal_address.setText(TextUtils.isEmpty(doctorDetail.getHospitalname()) ? "未填写" : doctorDetail.getHospitalname());//医院地址
        doctor_job_doc.setText(TextUtils.isEmpty(doctorDetail.getDepartment()) ? "未填写" : doctorDetail.getDepartment());//专业（科室）
        String[] association = doctorDetail.getAssociation();
        if (association != null && !association.equals("[]") && association.length > 0) {
            for (int i = 0; i < association.length; i++) { //协会
                TextView textView = new TextView(cnt);
                ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 80);
                marginLayoutParams.setMargins(15, 0, 15, 0);
                textView.setGravity(Gravity.CENTER);
                textView.setMinLines(40);
                textView.setTextColor(cnt.getResources().getColor(R.color.white));
                textView.setTextSize(12);
                textView.setGravity(Gravity.CENTER);
                doctor_hospital_doc.addView(textView, marginLayoutParams);
            }
        } else {
        }
        String professional = doctorDetail.getProfessional();
        doctor_good_doc.setText(TextUtils.isEmpty(professional) ? "未填写" : professional);//医生类别
        String attention = (doctorDetail.getAttention() == null) ? "0" : doctorDetail.getAttention();
        //   -1 不能关注, 0未关注，1已关注，2 不能取消关注
        if (attention.equals("0")) {
            btn_foucus_doc.setText("关注");
        } else if (attention.equals("1")) {
            btn_foucus_doc.setText("取消关注");
        } else if (attention.equals("-1")) {
            btn_foucus_doc.setText("关注");
        } else if (attention.equals("2")) {
            btn_foucus_doc.setText("取消关注");
        }
        setAttention(attention);

        tv_evaluation_rate.setText(TextUtils.isEmpty(doctorDetail.getOkay()) ? Html.fromHtml("<font color='#f0792d'>" + "0%" + "</font>") :
                Html.fromHtml("<font color='#f0792d'>" + doctorDetail.getOkay() + "%" + "</font>"));//好评率

        tv_sign_contract.setText(TextUtils.isEmpty(doctorDetail.getFnum()) ? Html.fromHtml("<font color='#f0792d'>" + 0 + "户" + "</font>") :
                Html.fromHtml("<font color='#f0792d'>" + doctorDetail.getFnum() + "户" + "</font>"));//签约家庭用户

        String healthy = doctorDetail.getHnum();
        tv_healthy_manager.setText(TextUtils.isEmpty(healthy) ? Html.fromHtml("<font color='#f0792d'>" + 0 + "人" + "</font>") :
                Html.fromHtml("<font color='#f0792d'>" + healthy + "人" + "</font>"));//健康管理人数

        String paymoney_ask = doctorDetail.getSign();//咨询人数
        tv_pay_money_ask.setText(TextUtils.isEmpty(paymoney_ask) ? Html.fromHtml("<font color='#f0792d'>" + 0 + "人" + "</font>") :
                Html.fromHtml("<font color='#f0792d'>" + paymoney_ask + "人" + "</font>"));//付费咨询

        tv_doctor_goodness_doc.setText(TextUtils.isEmpty(doctorDetail.getGoodremedy()) ? "未填写" : doctorDetail.getGoodremedy());//擅长
        tv_doctor_introduction_doc.setText(TextUtils.isEmpty(doctorDetail.getExperience()) ? "未填写" : doctorDetail.getExperience());//简介
        tv_query_doc_detail.setText("查看详情");//查看详情
        /*Integer tag = (Integer) tv_query_doc_detail.getTag();
        if(tag == null){
            tv_query_doc_detail.setText("查看详情");//查看详情
            tv_doctor_introduction_doc.setMaxLines(2);
            tv_doctor_introduction_doc.setEllipsize(TextUtils.TruncateAt.END);
            tv_query_doc_detail.setTag(R.drawable.arrow_down);
            ViewUtil.setTextImgBackground(tv_query_doc_detail, cnt, ViewUtil.TextMode.RIGHT, 0, R.drawable.arrow_down, 20);
        }else if(tag == R.drawable.arrow_up){
            tv_query_doc_detail.setText("查看详情");//查看详情
            tv_query_doc_detail.setTag(R.drawable.arrow_down);
            tv_doctor_introduction_doc.setMaxLines(2);
            ViewUtil.setTextImgBackground(tv_query_doc_detail, cnt, ViewUtil.TextMode.RIGHT, 0, R.drawable.arrow_down, 20);
        }else if(tag == R.drawable.arrow_down){
            tv_query_doc_detail.setText("向上收起");//查看详情
            tv_query_doc_detail.setTag(R.drawable.arrow_up);
            tv_doctor_introduction_doc.setMaxLines(20);
            ViewUtil.setTextImgBackground(tv_query_doc_detail, cnt, ViewUtil.TextMode.RIGHT, 0, R.drawable.arrow_up, 20);
        }*/
        tv_query_doc_detail.setText("向上收起");//查看详情
        tv_query_doc_detail.setTag(R.drawable.arrow_up);
        tv_doctor_introduction_doc.setMaxLines(20);
        ViewUtil.setTextImgBackground(tv_query_doc_detail, cnt, ViewUtil.TextMode.RIGHT, 0, R.drawable.arrow_up, 20);
    }

    public String getAttention() {
        return attention;
    }

    public void setAttention(String attention) {
        this.attention = attention;
    }
}
